<template>
  <Sign-up>
    <el-form
            ref="loginForm"
            label-width="80px"
            class="loginForm"
            :model="loginForm"
            :rules="loginRules">
      <el-form-item label="用户名" prop="username">
        <el-input placeholder="请输入用户名" v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input placeholder="请输入密码" type="password" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="submit_btn" @click="login">登录</el-button>
      </el-form-item>
      <div class="forgetPwd"><router-link to="/forgetPwd">忘记密码</router-link></div>
      <div class="tiparea">
        <p>
          还没有账号？现在
          <router-link to="/register">注册</router-link>
        </p>
      </div>
    </el-form>
  </Sign-up>
</template>

<script>
  import SignUp from "../components/SignUp"

  export default {
    name: "login",
    data() {
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        }
      }
    },
    methods: {
      login() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.$store.dispatch('login', this.loginForm)
              .then(() => {
                this.$message.success('登录成功')
                this.$router.push('/index')
              })
              .catch(err => {
                if (err.status === 404) {
                  //用户名不存在
                  this.$message.error(err.data.username)
                } else if (err.status === 400) {
                  //密码错误
                  this.$message.error(err.data.password)
                }
              })
          }
        })
      }
    },
    components: {
      SignUp
    }
  }
</script>

<style scoped lang="less">
  .loginForm {
    margin-top: 20px;
    background-color: #fff;
    padding: 20px 40px 20px 20px;
    border-radius: 5px;
    box-shadow: 0px 5px 10px #cccc;
  }

  .submit_btn {
    width: 100%;
  }

  .tiparea {
    text-align: center;
    font-size: 12px;
    color: #333;
  }

  .tiparea p a {
    color: #409eff;
  }

  .forgetPwd {
    font-size: 12px;
    margin-top: -10px;
    margin-bottom: 5px;
    text-align: right;
    a{
      color: #409eff;
    }
  }
</style>
